package com.basiclib.utils

import android.app.Activity
import android.app.Dialog
import android.content.ContextWrapper
import android.os.Build

fun Dialog.dismissExt() {
    if (isShowing) {
        val context = (context as ContextWrapper).baseContext
        if (context is Activity) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                if (!context.isFinishing && !context.isDestroyed) {
                    dismissWithExceptionHandling()
                }
            } else {
                if (!context.isFinishing) {
                    dismissWithExceptionHandling()
                }
            }
        } else
            dismissWithExceptionHandling()
    }
}

fun Dialog.dismissWithExceptionHandling() {
    try {
        dismiss()
    } catch (e: IllegalArgumentException) {
        // Do nothing.
    } catch (e: Exception) {
        // Do nothing.
    } finally {

    }
}